using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class ViewMaterialDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

        public ViewMaterialDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods		

		/// <summary>
        /// Selects all records from the ViewMaterial table.
		/// </summary>
        public List<ViewMaterialDTO> SelectAll()
		{
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_ViewMaterialSelectAll"))
			{
                List<ViewMaterialDTO> viewMaterialDTOList = new List<ViewMaterialDTO>();
				while (dataReader.Read())
				{
                    ViewMaterialDTO viewMaterialDTO = MapDataReader(dataReader);
                    viewMaterialDTOList.Add(viewMaterialDTO);
				}

                return viewMaterialDTOList;
			}
		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="repositoryId"></param>
        /// <returns></returns>
        public List<ViewMaterialDTO> SelectByRepository(string repositoryId)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@RepositoryId", repositoryId)
            };
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_ViewMaterialSelectByRepository", parameters))
            {
                List<ViewMaterialDTO> viewMaterialDTOList = new List<ViewMaterialDTO>();
                while (dataReader.Read())
                {
                    ViewMaterialDTO viewMaterialDTO = MapDataReader(dataReader);
                    viewMaterialDTOList.Add(viewMaterialDTO);
                }

                return viewMaterialDTOList;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="repositoryId"></param>
        /// <param name="materialId"></param>
        /// <returns></returns>
        public ViewMaterialDTO SelectByRepositoryAndMaterial(string repositoryId, string materialId)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@RepositoryId", repositoryId),
                new SqlParameter("@MaterialId", materialId)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_ViewMaterialSelectByRepositoryAndMaterial", parameters))
            {
                if (dataReader.Read())
                {
                    return MapDataReader(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }

		/// <summary>
        /// Creates a new instance of the ViewMaterialDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private ViewMaterialDTO MapDataReader(SqlDataReader dataReader)
		{
            ViewMaterialDTO viewMaterialDTO = new ViewMaterialDTO();
            viewMaterialDTO.MaterialId = dataReader.GetString("MaterialId", String.Empty);
            viewMaterialDTO.MaterialName = dataReader.GetString("MaterialName", null);
            viewMaterialDTO.Address = dataReader.GetString("Address", null);
            viewMaterialDTO.UnitId = dataReader.GetString("UnitId", null);
            viewMaterialDTO.UnitName = dataReader.GetString("UnitName", String.Empty);
            viewMaterialDTO.PriceByDay = dataReader.GetInt32("PriceByDay", 0);
            viewMaterialDTO.Amount = dataReader.GetInt32("Amount", 0);
            viewMaterialDTO.Remaining = dataReader.GetInt32("Remaining", 0);
            viewMaterialDTO.RepositoryId = dataReader.GetString("RepositoryId", String.Empty);
            viewMaterialDTO.RepositoryName = dataReader.GetString("RepositoryName", String.Empty);
            return viewMaterialDTO;
		}

		#endregion
	}
}
